package com.houhanzhi.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.houhanzhi.enums.DomainTypeEnum;
import com.houhanzhi.manager.DomainManager;
import com.houhanzhi.mapper.DomainMapper;
import com.houhanzhi.model.DomainDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class DomainManagerImpl implements DomainManager {

    @Autowired
    private DomainMapper domainMapper;

    /**
     * 根据id查找详情
     *
     * @param id
     * @param accountNo
     * @return
     */
    @Override
    public DomainDO findById(Long id, Long accountNo) {
        return domainMapper.selectOne(new QueryWrapper<DomainDO>()
                .eq("id", id)
                .eq("account_no", accountNo));
    }

    /**
     * 根据域名类型和id查找详情
     *
     * @param id
     * @param domainTypeEnum
     * @return
     */
    @Override
    public DomainDO findByDomainTypeAndId(Long id, DomainTypeEnum domainTypeEnum) {
        return domainMapper.selectOne(new QueryWrapper<DomainDO>()
                .eq("id", id)
                .eq("domain_type", domainTypeEnum.name())
        );
    }

    /**
     * 新增域名
     *
     * @param domainDO
     * @return
     */
    @Override
    public int addDomain(DomainDO domainDO) {
        return domainMapper.insert(domainDO);
    }

    /**
     * 列举全部官方域名
     *
     * @return
     */
    @Override
    public List<DomainDO> listOfficialDomain() {
        return domainMapper.selectList(new QueryWrapper<DomainDO>().eq("domain_type", DomainTypeEnum.OFFICIAL.name()));
    }

    /**
     * 列举全部自定义域名
     * @param accountNo
     * @return
     */
    @Override
    public List<DomainDO> listCustomDomain(Long accountNo) {
        return domainMapper.selectList(new QueryWrapper<DomainDO>()
                .eq("account_no", accountNo)
                .eq("domain_type", DomainTypeEnum.CUSTOM.name()));
    }
}
